<!-- 我的 -->
<template>
  <div class="user">
    <!-- 头部 -->
    <div class="user_top">
      <img src="../../assets/images/profilePicture.png" width="70" alt="" />
      <h3 v-if="logined">{{ username }}</h3>
      <h3 @click="showModal = true" v-else>点击登录</h3>
      <van-icon :name="logined ? 'cross' : 'arrow'" @click="loginout" />
    </div>
    <!-- 九宫格部分 -->
    <van-grid square :column-num="3">
      <van-grid-item v-for="item in gridArr" :key="item.id" :icon="item.icon" :text="item.type" @click="btn(item.id)"/>
    </van-grid>
    <!-- 登录框 -->
    <div class="modal" v-show="showModal">
      <div class="modal_bg" @click="showModal = false"></div>
      <div class="modal_content">
        <van-form @submit="onSubmit">
          <van-field v-model="username" name="username" label="用户名" placeholder="用户名" :rules="[{ required: true, message: '请填写用户名' }]"/>
          <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]"/>
          <div style="margin: 16px">
            <van-button round block type="danger" native-type="submit">提交</van-button>
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
// 引入登录接口
import IndexAx from './axios';
import {mapMutations} from 'vuex';
const gologin = new IndexAx;
export default {
  data() {
    return {
      logined: false, // 登录状态
      gridArr: [
        // grid数组
        { id: 0, icon: "label-o", type: "我的订单" },
        { id: 1, icon: "bill-o", type: "优惠券" },
        { id: 2, icon: "goods-collect-o", type: "礼品卡" },
        { id: 3, icon: "location-o", type: "我的收藏" },
        { id: 4, icon: "flag-o", type: "我的足迹" },
        { id: 5, icon: "contact", type: "会员福利" },
        { id: 6, icon: "aim", type: "地址管理" },
        { id: 7, icon: "warn-o", type: "账号安全" },
        { id: 8, icon: "service-o", type: "联系客服" },
        { id: 9, icon: "question-o", type: "帮助中心" },
        { id: 10, icon: "smile-comment-o", type: "意见反馈" },
      ],
      showModal: false, // 是否显示登录框
      username: "", // 用户名
      password: "", // 密码
    };
  },
  created() {
    // 登陆前先看本人是否登陆过
    let userInfo = sessionStorage.getItem("userInfo");
    if (userInfo) {
      this.logined = true;
      userInfo = JSON.parse(userInfo);
      this.username = userInfo;
    }
  },
  methods: {
    ...mapMutations('login' , ['setToken']),
    loginout() {
      //退出登录
      if (this.logined) {
        this.$dialog
          .confirm({
            title: "退出账号确认",
            message: "您是否想要退出当前账号？",
          })
          .then(() => {
            // 清除token
            sessionStorage.removeItem("token");
            sessionStorage.removeItem("userInfo");
            // 刷新当前页
            this.$router.go(0);
          });
      } else {
        // 代表未登录，要打开模态框
        this.showModal = true;
      }
    },
    //登录提交按钮
    onSubmit(values) {
      gologin.Rotation({
          username: values.username,
          password: values.password,
        }).then((res) => {
          console.log(res)
          if (res.status == 200) {
            this.$toast.success("登录成功");
            // 存储token
            this.setToken(res.data.token)
            // 关闭登录框
            this.showModal = false;
            // 存储userInfo
            let str = JSON.stringify(res.data.username);
            sessionStorage.setItem("userInfo", str);
            this.username = res.data.username;
            // 修改为已经登录
            this.logined = true;
          }
          if (res.status == 400) {
            alert('用户名或密码错误')
          }
        });
      },
      btn(id) {

        // 跳转到地址address页面
        switch (id) {
            case 0:
              //订单
                this.$router.push({name:"order"})
                return
            default:
              return;
        }
      },
    },
};
</script>

<style lang='less' scoped>
.user {
  .user_top {
    background: #333;
    color: #fff;
    padding: 20px 10px;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      border-radius: 50%;
      margin-right: 10px;
    }
    h3 {
      flex: 1;
    }
  }
  .modal {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    left: 0;
    top: 0;
    .modal_bg {
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
    }
    .modal_content {
      width: 90%;
      height: 200px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 40px;
    }
  }
}
</style>